package com.beyondthewalls.components;

import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.physics.PhysicsComponent;
import com.almasb.fxgl.physics.SensorCollisionHandler;
import com.almasb.fxgl.physics.box2d.dynamics.BodyType;
import com.beyondthewalls.utils.Direction;
import java.util.HashSet;
import java.util.Set;

public class Air extends Component {
    private final double width, height;
    private Direction direction;
    private static final int power = 200;
    Set<Component> pushComponents = new HashSet<>();

    public Air(double width, double height, int d) {
        this.width = width;
        this.height = height;

        switch (d) {
            case 1:
                direction = Direction.up;
                break;
            case 2:
                direction = Direction.down;
                break;
            case 3:
                direction = Direction.left;
                break;
            case 4:
                direction = Direction.right;
                break;
        }
    }

    @Override
    public void onAdded() {
        PhysicsComponent physics = new PhysicsComponent();
        physics.setBodyType(BodyType.STATIC);

        physics.addSensor(new HitBox(BoundingShape.box(width, height)),
                new SensorCollisionHandler() {
                    @Override
                    protected void onCollisionBegin(Entity other) {
                        if (other.hasComponent(CatJump.class)) {
                            CatJump cj = other.getComponent(CatJump.class);
                            pushComponents.add(cj);
                        } else if (other.hasComponent(Player.class)) {
                            Player player = other.getComponent(Player.class);
                            pushComponents.add(player);
                        }
                    }

                    @Override
                    protected void onCollisionEnd(Entity other) {
                        if (other.hasComponent(CatJump.class)) {
                        CatJump cj = other.getComponent(CatJump.class);
                        pushComponents.remove(cj);
                        }  else if (other.hasComponent(Player.class)) {
                            Player player = other.getComponent(Player.class);
                            pushComponents.remove(player);
                        }
                    }
        });

        entity.addComponent(physics);
    }

    @Override
    public void onUpdate(double tpf) {
        if (!pushComponents.isEmpty()) {
            for (Component c : pushComponents) {
                if (c instanceof Player player) {
                    player.setCanMove(tpf * 2);

                    switch (direction) {
                        case up:
                            player.pushY(-power * 0.5);
                            break;
                        case down:
                            player.pushY(power);
                            break;
                        case left:
                            player.pushX(-power);
                            break;
                        case right:
                            player.pushX(power);
                            break;
                    }
                } else if (c instanceof CatJump cj) {
                    switch (direction) {
                        case up:
                            cj.pushY(-power);
                            break;
                        case down:
                            cj.pushY(power);
                            break;
                        case left:
                            cj.pushX(-power);
                            break;
                        case right:
                            cj.pushX(power);
                            break;
                    }
                }
            }
        }
    }
}
